package com.mgm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mgm.character.service.CharactersService;
import com.mgm.exception.CustomException;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Characters;
import com.mgm.result.IMessage;
import com.mgm.result.IResult;


/**
 * 角色Controller
 * <p>Title: CharactersController</p>
 * <p>Description: </p>
 * <p>Company: MGM</p> 
 * @author	Andy
 * @date	2018年6月20日上午10:52:43
 * @version 1.0
 */
@RestController
@RequestMapping("/characters")
public class CharactersController extends BaseController {
	
	private static Logger logger =LoggerFactory.getLogger(CharactersController.class);

	@Autowired
	private CharactersService charactersService;
	
	/**
	 * 
	 * @Title: listCharacters   
	 * @Description: 角色列表   
	 * @param: @param pageNum
	 * @param: @param pageSize
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<DataGrid>>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<IResult<DataGrid>> listCharacters(@RequestParam(value="page", defaultValue="1") Integer pageNum,
			@RequestParam(value="ps", defaultValue="10") Integer pageSize, HttpServletRequest request, HttpServletResponse response) {
		DataGrid dg = charactersService.findAll(pageNum, pageSize);
		IResult<DataGrid> result = IResult.success(dg);
		
		return new ResponseEntity<IResult<DataGrid>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: createCharacters   
	 * @Description: 添加角色   
	 * @param: @param characters
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.POST) 
	public ResponseEntity<IMessage> createCharacters(@RequestBody Characters characters){
		List<Characters> list = charactersService.findByName(characters.getName());
		if(!CollectionUtils.isEmpty(list)) {
			throw new CustomException("角色名已存在");
		}
		IMessage msg= charactersService.saveCharacters(characters, getCurUser());
		return new ResponseEntity<IMessage>(msg, HttpStatus.CREATED);
	}
	
	/**
	 * 
	 * @Title: updateCharacters   
	 * @Description: 修改角色信息
	 * @param: @param characters
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> updateCharacters(@RequestBody Characters characters){
		if(characters.getId()==null || characters.getId() <= 0) {
			logger.error("Illegal operation, Characters update fail!");
			throw new CustomException("非法操作，更新失败");
		}
		Characters ct = charactersService.findByKey(characters.getId());
		if(ct==null) {
			logger.error("Unable to update. Characters with id " + characters.getId() + " not found");
			throw new CustomException("角色不存在，更新失败");
		}
		IMessage msg= charactersService.updateCharacters(characters, getCurUser());
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: getCharacters   
	 * @Description: 角色详情   
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<Characters>>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResponseEntity<IResult<Characters>> getCharacters(@PathVariable("id") Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		Characters characters = charactersService.findByKey(id);
		IResult<Characters> result = IResult.success(characters);
		return new ResponseEntity<IResult<Characters>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: deleteCharacters   
	 * @Description: 删除角色
	 * @param: @param id
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
	public ResponseEntity<IMessage> deleteCharacters(@PathVariable("id") Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		Characters characters = charactersService.findByKey(id);
		if(characters==null) {
			logger.error("Unable to delete. Characters with id " + id + " not found");
			throw new CustomException("角色不存在，删除失败");
		}
		IMessage msg= charactersService.deleteByKey(id);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
}
